package com.zhixiang.xaec.service.safebase.securitySystem.impl;

import java.lang.reflect.InvocationTargetException;

import javax.annotation.Resource;

import org.apache.commons.beanutils.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.zhixiang.core.dao.BaseDao;
import com.zhixiang.core.service.impl.BaseServiceImpl;
import com.zhixiang.xaec.dao.safebase.securitySystem.InstitutionalArchivesDao;
import com.zhixiang.xaec.entity.safebase.securitySystem.InstituArchRev;
import com.zhixiang.xaec.entity.safebase.securitySystem.InstitutionalArchives;
import com.zhixiang.xaec.service.safebase.securitySystem.InstituArchRevService;
import com.zhixiang.xaec.service.safebase.securitySystem.InstitutionalArchivesService;
@Service
public class InstitutionalArchivesServiceImpl extends 
BaseServiceImpl<InstitutionalArchives,Long> implements InstitutionalArchivesService{

    @Override
    @Resource(type=InstitutionalArchivesDao.class)
    public void setBaseDao(BaseDao<InstitutionalArchives, Long> dao) {
       setDao(dao);
    }
    
    @Autowired
    private InstituArchRevService instituArchRevService;

    @Override
    public String delete(String[] ids)
    {
       for(String id:ids){
           getDao().remove(Long.valueOf(id));
       }
        return null;
    }

    @Override
    public String saveArch(InstitutionalArchives institutionalArchives,String state) {
        
            getDao().save(institutionalArchives);
            
            if(instituArchRevService.getInstitutionalId(Long.toString(institutionalArchives.getId()))==null){
                InstituArchRev instituArchRev=new InstituArchRev();
                try {
                    BeanUtils.copyProperties(instituArchRev, institutionalArchives);
                    instituArchRev.setId(null);
                    instituArchRev.setInstitutionalId(institutionalArchives.getId());
                    instituArchRev.setState("新增制度");
                    instituArchRevService.save(instituArchRev);
                } catch (IllegalAccessException e) {
                    e.printStackTrace();
                } catch (InvocationTargetException e) {
                    e.printStackTrace();
                } 
            }
            if("editSave".equals(state)){
                InstituArchRev instituArchRev=new InstituArchRev();
                try {
                    BeanUtils.copyProperties(instituArchRev, institutionalArchives);
                    instituArchRev.setId(null);
                    instituArchRev.setInstitutionalId(institutionalArchives.getId());
                    instituArchRev.setState("修订制度");
                    instituArchRevService.save(instituArchRev);
                } catch (IllegalAccessException e) {
                    e.printStackTrace();
                } catch (InvocationTargetException e) {
                    e.printStackTrace();
                } 
            }
           
         return null;
    }
}
